package com.bosssoft.cloud.wechat.core.controller;

import com.bosssoft.cloud.wechat.core.api.SignatureService;
import com.bosssoft.cloud.wechat.core.bean.signature.WxSignatureVO;
import com.bosssoft.cloud.wechat.core.infra.config.WxConfig;
import com.bosssoft.cloud.wechat.core.infra.util.WxSignatureUtil;
import com.bosssoft.cloud.wechat.infrastructure.bean.response.R;
import com.bosssoft.cloud.wechat.infrastructure.enums.ResultEnum;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 权限签名控制器
 *
 * @author wcy create on 2021/7/1 10:24 上午
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "signature")
public class SignatureController {

    @Resource
    private WxConfig wxConfig;

    @Autowired
    private SignatureService signatureService;

    @ApiOperation("获取WXJsapiTicket")
    @GetMapping("getWXJsapiTicket")
    public R<?> getWXJsapiTicket(@RequestParam String url) {
        String timestamp = String.valueOf(WxSignatureUtil.getCurrentTimestamp());
        String noncestr = WxSignatureUtil.generateNonceStr();
        return new R<>(ResultEnum.SUCCESS, WxSignatureVO.builder()
                .appId(wxConfig.getMpConfig().getMpAppId())
                .nonceStr(noncestr)
                .timestamp(timestamp)
                .signature(signatureService.getSignature(timestamp, noncestr, url))
                .build());
    }
}
